#!/bin/bash

# 一键部署脚本
# 使用方法: ./deploy.sh [环境] [选项]
# 环境: dev, prod (默认: prod)
# 选项: --build, --up, --down, --logs, --restart

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_header() {
    echo -e "${BLUE}================================${NC}"
    echo -e "${BLUE}  Official Styled Helper 部署脚本${NC}"
    echo -e "${BLUE}================================${NC}"
}

# 检查Docker是否安装
check_docker() {
    if ! command -v docker &> /dev/null; then
        print_error "Docker 未安装，请先安装 Docker"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        print_error "Docker Compose 未安装，请先安装 Docker Compose"
        exit 1
    fi
    
    print_message "Docker 环境检查通过"
}

# 检查环境变量文件
check_env_file() {
    if [ ! -f ".env.production" ]; then
        print_warning "未找到 .env.production 文件"
        if [ -f "env.production.template" ]; then
            print_message "正在创建 .env.production 文件..."
            cp env.production.template .env.production
            print_warning "请编辑 .env.production 文件，填入正确的配置信息"
            print_warning "特别是 AI API 密钥等敏感信息"
            read -p "按 Enter 键继续，或 Ctrl+C 退出编辑配置..."
        else
            print_error "未找到环境变量模板文件"
            exit 1
        fi
    fi
    print_message "环境变量文件检查通过"
}

# 构建镜像
build_image() {
    print_message "开始构建 Docker 镜像..."
    docker-compose build --no-cache
    print_message "Docker 镜像构建完成"
}

# 启动服务
start_services() {
    print_message "启动服务..."
    docker-compose up -d
    print_message "服务启动完成"
    
    # 等待服务启动
    print_message "等待服务启动..."
    sleep 10
    
    # 检查服务状态
    if docker-compose ps | grep -q "Up"; then
        print_message "服务运行正常"
        print_message "应用访问地址: http://localhost:3000"
    else
        print_error "服务启动失败，请检查日志"
        docker-compose logs
        exit 1
    fi
}

# 停止服务
stop_services() {
    print_message "停止服务..."
    docker-compose down
    print_message "服务已停止"
}

# 重启服务
restart_services() {
    print_message "重启服务..."
    docker-compose restart
    print_message "服务重启完成"
}

# 查看日志
show_logs() {
    print_message "显示服务日志..."
    docker-compose logs -f
}

# 清理资源
cleanup() {
    print_message "清理未使用的 Docker 资源..."
    docker system prune -f
    print_message "清理完成"
}

# 显示帮助信息
show_help() {
    echo "使用方法: $0 [命令] [选项]"
    echo ""
    echo "命令:"
    echo "  build     构建 Docker 镜像"
    echo "  up        启动服务"
    echo "  down      停止服务"
    echo "  restart   重启服务"
    echo "  logs      查看日志"
    echo "  status    查看服务状态"
    echo "  cleanup   清理 Docker 资源"
    echo "  help      显示帮助信息"
    echo ""
    echo "选项:"
    echo "  --no-cache    构建时不使用缓存"
    echo "  --follow      查看日志时跟随输出"
    echo ""
    echo "示例:"
    echo "  $0 build          # 构建镜像"
    echo "  $0 up             # 启动服务"
    echo "  $0 logs --follow  # 查看实时日志"
    echo "  $0 down           # 停止服务"
}

# 显示服务状态
show_status() {
    print_message "服务状态:"
    docker-compose ps
    echo ""
    print_message "Docker 资源使用情况:"
    docker stats --no-stream
}

# 主函数
main() {
    print_header
    
    # 检查Docker环境
    check_docker
    
    # 检查环境变量文件
    check_env_file
    
    # 解析参数
    COMMAND=${1:-"up"}
    
    case $COMMAND in
        "build")
            build_image
            ;;
        "up")
            build_image
            start_services
            ;;
        "down")
            stop_services
            ;;
        "restart")
            restart_services
            ;;
        "logs")
            show_logs
            ;;
        "status")
            show_status
            ;;
        "cleanup")
            cleanup
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            print_error "未知命令: $COMMAND"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
